import 'package:flutter/material.dart';

class RouterTestRoute extends StatelessWidget {
  const RouterTestRoute({Key? key}) : super(key: key);

  @override
  /**
   * 学到的知识
   * 1.动态路由传值
   * Navigator.push(context,MaterialPageRoute(
   * builder:(context){
   * return const TipRoute(
   * text:'参数')}
   * ))
  */
  Widget build(BuildContext context) {
    String _back;
    return Center(
      child: ElevatedButton(
        child: const Text("打开提示页"),
        onPressed: () async {
          var result = await Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) {
                return const TipRoute(text: "我是提示xxxx", id: "10");
              },
            ),
          );
          // print('back$result["back"]');
          _back = result["back"];
          print(_back);
        },
      ),
    );
  }
}

class TipRoute extends StatelessWidget {
  const TipRoute({Key? key, required this.text, required this.id // 接收一个text参数
      })
      : super(key: key);
  final String text;
  final String id;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("提示"),
      ),
      body: Padding(
        padding: const EdgeInsets.all(18),
        child: Center(
          child: Column(
            children: <Widget>[
              Text(text),
              Text(id),
              ElevatedButton(
                onPressed: () =>
                    Navigator.pop(context, {"back": "我是返回值"}), //从子页面返回到主页面，传参
                child: const Text("返回"),
              )
            ],
          ),
        ),
      ),
    );
  }
}
